package cn.m1c.cntt.wuhan2020.cnttlifegoods.order.controller

import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.component.Security
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.enums.StatusCode
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.model.ResultVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.utils.AssertUtil
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.utils.getAuthUser
import cn.m1c.cntt.wuhan2020.cnttlifegoods.order.service.OrderService
import cn.m1c.cntt.wuhan2020.cnttlifegoods.order.vo.OrderBuyerGoodsVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.order.vo.OrderBuyerVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.order.vo.OrderVo
import com.alibaba.fastjson.JSON
import org.apache.logging.log4j.kotlin.Logging
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import java.util.*
import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

@Controller
@RequestMapping("/order")
@CrossOrigin(origins = ["*"])
class OrderController: Logging {
    @Resource
    lateinit var orderService: OrderService

    @GetMapping("/list" )
    @ResponseBody
    fun list(request: HttpServletRequest, response: HttpServletResponse):String{
        var list = orderService.list(OrderVo())
        var resultVo = ResultVo<List<OrderVo>>(data = list)
        return JSON.toJSONString(resultVo)
    }
    @PostMapping("/save" )
    @ResponseBody
    @Security
    fun save(request: HttpServletRequest, response: HttpServletResponse,@RequestBody orderVo: OrderVo):String{
        var userVo = getAuthUser(request)
        orderVo.userId = userVo.id
        var resultVo = orderService.save(orderVo)
        return JSON.toJSONString(resultVo)
    }


    @GetMapping("/info" )
    @ResponseBody
    @Security
    fun info(request: HttpServletRequest, response: HttpServletResponse,id:String):String{
        var userVo = getAuthUser(request)
        var orderVo = orderService.findById(id)

        var attribute = HashMap<String,Any>()
        if(userVo.id.equals(orderVo.userId)){
            attribute.put("editStatus",true)
        }else{
            attribute.put("editStatus",false)
        }
        //查询当前用户已经选择的小区
        var orderBuyerVo = orderService.findBuyerByUserIdAndOrderId(userVo.id!!,orderVo.id!!)
        if(orderBuyerVo!= null){
            orderVo.areaList?.forEach {
                if(it.areaName.equals(orderBuyerVo.areaName)){
                    it.areaStatus = 1
                }
            }
            //查询当前用户已经设置的采购数量
            var buyerGoodsVoMap = HashMap<String,Double>()
            if(orderBuyerVo.goodsList?.size!! > 0){
                orderBuyerVo.goodsList!!.forEach{
                    buyerGoodsVoMap.put(it.goodsName!!,it.goodsCount!!)
                }
            }
            orderVo.goodsList?.forEach {
                if(buyerGoodsVoMap.containsKey(it.goodsName)){
                    it.goodsCount = buyerGoodsVoMap.get(it.goodsName)
                }
            }
        }
        var resultVo = ResultVo<OrderVo>(data = orderVo,attribute = attribute)
        return JSON.toJSONString(resultVo)
    }

    @GetMapping("/buyerInfo" )
    @ResponseBody
    @Security
    fun buyerInfo(request: HttpServletRequest, response: HttpServletResponse,id:String):String{
        var userVo = getAuthUser(request)
        var orderVo = orderService.findById(id)
        AssertUtil.isTrue(userVo.id.equals(orderVo.userId),StatusCode.failed,"您没有权限查看")
        //查询当前用户已经选择的小区
        var orderBuyerVoList = orderService.findBuyer()
        var buyerInfo = HashMap<String,Any>()
        var areaUserGoodsMap = HashMap<String,ArrayList<OrderBuyerVo>>()
        if(orderBuyerVoList!= null){
            buyerInfo.put("total",orderBuyerVoList.size)
            var totalMoney = 0.00
            var goodsMap = HashMap<String,Double>()
            orderBuyerVoList.forEach{
                var orderBuyerVo = it
                //所有商品列表
                orderBuyerVo.goodsList?.forEach { goods ->
                    totalMoney +=  goods.goodsPrice!! * goods.goodsCount!!
                    var goodsMapKey = goods.goodsName
                    if(goodsMap.containsKey(goodsMapKey)){
                        goodsMap[goodsMapKey!!] = goodsMap[goodsMapKey]!!+goods.goodsCount!!
                    }else{
                        goodsMap[goodsMapKey!!] = goods.goodsCount!!
                    }
                }
                if(areaUserGoodsMap.containsKey(orderBuyerVo.areaName)){
                }else{
                    areaUserGoodsMap[orderBuyerVo.areaName!!] = ArrayList()
                }
                areaUserGoodsMap[orderBuyerVo.areaName!!]?.add(orderBuyerVo)

            }
            buyerInfo.put("totalMoney",totalMoney)
        }
        var resultVo = ResultVo<HashMap<String,ArrayList<OrderBuyerVo>>>(data = areaUserGoodsMap,attribute = buyerInfo)
        return JSON.toJSONString(resultVo)
    }


    @PostMapping("/saveBuyerGoods" )
    @ResponseBody
    @Security
    fun saveBuyerGoods(request: HttpServletRequest, response: HttpServletResponse,@RequestBody orderBuyerVo: OrderBuyerVo):String{
        var userVo = getAuthUser(request)
        orderBuyerVo.userId = userVo.id
        orderBuyerVo.nickName = userVo.nickName
        AssertUtil.hasLength(orderBuyerVo.orderId,StatusCode.failed,"orderId 不能为空")
        AssertUtil.hasLength(orderBuyerVo.phone,StatusCode.failed,"phone 不能为空")
        AssertUtil.hasLength(orderBuyerVo.areaName,StatusCode.failed,"小区 不能为空")
        AssertUtil.hasLength(orderBuyerVo.address,StatusCode.failed,"小区里的详细地址 不能为空")
        AssertUtil.isNotNull(orderBuyerVo.goodsList,StatusCode.failed,"goodsList 不能为空")
        orderService.saveBuyer(orderBuyerVo)
        var resultVo = ResultVo<OrderVo>(data = null)
        return JSON.toJSONString(resultVo)
    }


    @PostMapping("/updateStatus" )
    @ResponseBody
    @Security
    fun updateStatus(request: HttpServletRequest, response: HttpServletResponse,@RequestBody orderVo: OrderVo):String{
        var userVo = getAuthUser(request)
        AssertUtil.hasLength(orderVo.id,StatusCode.failed,"id 不能为空")
        AssertUtil.isNotNull(orderVo.orderStatus,StatusCode.failed,"orderStatus 不能为空")
        orderService.updateOrderStatus(orderVo,userVo.id!!)
        var resultVo = ResultVo<OrderVo>(data = null)
        return JSON.toJSONString(resultVo)
    }
}